<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//管理员
class Admin extends CommonController {

	function __construct(){
		
		parent::__construct();
		$this->load->model('User/AdminModel','CurModel');
		
		$this->load->model('Index/RoleModel','RoleModel');
		$this->role_arr = $this->RoleModel->getAll();
	}
	
	
	//start
	/**
	* @name 获取列表
	* @param page ture char 页面
	* @param size ture char 数量
	* @return 成功返回1
	*/
	//end
	function get_list(){
		$user_id = $this->user_id;
		
		$filter = array();
		$arr = array(
			'filter'=>$filter
		);
		
		$result = parent::_get_list($arr);
		if(!$result){
			$this->err_out($this->getError());
		}

		
		$this->data_out($result);
	}
	
	function info_out($info){
		$info['last_login'] = date('Y-m-d H:i:s',$info['last_login']);
		$info['addtime'] = date('Y-m-d H:i:s',$info['addtime']);
		
		$info = $this->data_filter($info,null,array('password'));
		
		$info['role_name']='';
		foreach($this->role_arr as $val){
			if($val['id']==$info['role_id']){
				$info['role_name'] = $val['name'];
				break;
			}
		}
		
		return $info;
	}
	
	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function get_info(){
		$user_id = $this->user_id;
		
		$result = parent::_get_info();
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->data_out($result);
	}
	
	
	//start
	/**
	* @name 获取我的信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function get_my_info(){
		$user_id = $this->user_id;
		
		$result = $this->CurModel->_id_get_info($user_id);
		
		$result = $this->info_out($result);
	
		$result['privs'] = $this->privs;
		
		
		$this->load->dmodel('user_login_log','user_login_log');
		
		$res = $this->user_login_log->getAll("user_id='{$user_id}'","*",2,0,'addtime','desc');
		$item = end($res);
		$result['prev_time'] = $item['addtime']?date('Y-m-d H:i:s',$item['addtime']):'-';
		
		
		$result['login_region'] = $item['region'];
		$result['login_ip'] = $item['ip'];
		
		$this->data_out($result);
	}
	
	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function save(){
		
		if(!$this->check_priv('admin_update|admin_add')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$filter = array('role_id'=>'请选择角色');
		if(isset($_POST['role_id']) && empty($_POST['role_id'])){
			$this->err_out('请选择角色');
		}
		$post = array('nickname','role_id');
		$farr = array(
			'post'=>$post,
			'filter'=>$filter
		);
		
		$result = parent::_save($farr);
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->add_log("保存管理员【{$result['id']}】信息",'admin');
		
		$this->data_out($result);
	}
	
	//start
	/**
	* @name 密码变更
	* @param old_password ture int 旧密码
	* @param password ture char 密码
	* @param confirm_password ture char 确认密码
	* @return 成功返回1
	*/
	//end
	function change_password(){
		$user_id = $this->user_id;
		if(!$this->check_priv('admin_update')){
			$this->err_out(LANG_NOPRIV);
		}

		$old_password = $this->input->post('old_password',null,'');
		$password = $this->input->post('password',null,'');
		$confirm_password = $this->input->post('confirm_password',null,'');
		if($password===''){
			$this->err_out('密码不能为空');
		}
		if(mb_strlen($password)<6 || mb_strlen($password)>18){
			$this->err_out('请输入位数在6至18之间的密码');
		}
		
		if($password!==$confirm_password){
			$this->err_out('两次密码输入不一样');
		}
		
		$info = $this->CurModel->get_info($user_id);
		if(!$info){
			$this->err_out('用户不存在');
		}
		$old_password_md5 = md5($old_password);
		if($info['password']!==$old_password_md5){
			$this->err_out('旧密码输入错误');
		}
		
		$pwd = md5($password);
		$this->CurModel->edit(array('password'=>$pwd),"id='{$user_id}'");
		
		$this->add_log("员工【{$info['username']}】密码变更",'admin');
		
		$this->data_out(1);
	}
	
	//start
	/**
	* @name 添加管理员
	* @param id ture int ID
	* @param password ture char 密码
	* @param confirm_password ture char 确认密码
	* @return 成功返回1
	*/
	//end
	function add(){
		
		if(!$this->check_priv('admin_add')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$password = $this->input->post('password',null,'');
		$confirm_password = $this->input->post('confirm_password',null,'');
		if($password===''){
			$this->err_out('密码不能为空');
		}
		if(mb_strlen($password)<6 || mb_strlen($password)>18){
			$this->err_out('请输入位数在6至18之间的密码');
		}
		
		if($password!==$confirm_password){
			$this->err_out('两次密码输入不一样');
		}
		
		$pwd = md5($password);
		$time = time();
		$arr = array(
			'username'=>$this->input->post('username',null,''),
			'nickname'=>$this->input->post('nickname',null,''),
			'password'=>$pwd,
			'addtime'=>$time,
			"role_id"=>$this->input->post('role_id',null,'')
		);
		
		$data = array(
			'username'=>'账号不能为空',
			'role_id'=>'请选择角色'
		);
		$result = $this->CheckData($data,$arr,true);
		if($result!==false){
			$this->err_out($result);
		}
		$result = $this->CurModel->_where_exist(array('username'=>$arr['username']));
		if($result){
			$this->err_out('不能设置重复的账号');
		}
		
		
		$id = $this->CurModel->add($arr);
		
		
		$this->add_log("添加管理员【{$id}】",'admin');
		$this->data_out(1);
	}
	//start
	/**
	* @name 删除
	* @param id ture int ID
	* @return 成功返回1
	*/
	//end
	function del(){
		
		if(!$this->check_priv('admin_del')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$result = parent::_del();
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->add_log("删除管理员【{$result}】",'admin');
		$this->data_out($result);
		
	}
	
}
